/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.ekuma.epos.datalogic.define.dao;

import com.openbravo.data.loader.TableDBSentenceBuilder;
import e.odbo.data.dao.BaseDAO;
import cn.ekuma.epos.db.table.I_Place;

import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.serialize.DataRead;
import com.openbravo.data.loader.serialize.DataWrite;
import com.openbravo.data.loader.serialize.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.sentence.PreparedSentence;
import com.openbravo.data.loader.sqlbuilder.QBFBuilder;
import com.openbravo.data.loader.serialize.serializer.write.SerializerWriteBasic;
import com.openbravo.data.loader.serialize.serializer.write.SerializerWriteBasicExt;
import com.openbravo.data.loader.serialize.serializer.write.SerializerWriteString;

import com.openbravo.data.loader.sentence.StaticSentence;
import com.openbravo.data.loader.table.TableDefinition;
import com.openbravo.format.Formats;
import com.openbravo.bean.Place;
import com.openbravo.data.model.Field;
import java.util.List;

/**
 *
 * @author Administrator
 */
public class PlaceDAO extends BaseDAO<Place>{

    public PlaceDAO(I_Session s) {
        super(s);
    }

    
    @Override
    public TableDefinition getTable() {
        return new TableDefinition(
                "PLACES",
                new Field[]{
                    new Field(I_Place.ID, Datas.STRING, Formats.STRING),
                    new Field(I_Place.NAME, Datas.STRING, Formats.STRING),
                    new Field(I_Place.X, Datas.INT, Formats.INT),
                    new Field(I_Place.Y, Datas.INT, Formats.INT),
                    new Field(I_Place.FLOOR, Datas.STRING, Formats.STRING) 
                },
                new int[]{0});
    }

    @Override
    public void writeInsertValues(DataWrite dp, Place obj) throws BasicException {
       dp.setString(1, obj.getId());
       dp.setString(2, obj.getName());
       dp.setInt(3, obj.getX());
       dp.setInt(4, obj.getY());
       dp.setString(5, obj.getFloor());
      
    }


    public Place readValues(DataRead dr,Place obj) throws BasicException {
    	if(obj==null)
    		obj=new Place();
    	obj.setM_sId(dr.getString(1));
    	obj.setM_sName(dr.getString(2));
    	obj.setM_ix(dr.getInt(3));
    	obj.setM_iy(dr.getInt(4));
    	obj.setM_sfloor(dr.getString(5));
        return obj;
    }


    public List<Place> list(Object filter) throws BasicException {
       return new StaticSentence(s, new QBFBuilder(
                ((TableDBSentenceBuilder)getTableBuilder()).getListSQL(getTable())+" WHERE ?(QBF_FILTER) ORDER BY ID", new String[]{"ID", "NAME", "FLOOR"}), new SerializerWriteBasic(new Datas[]{Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.STRING}), this).list(filter);
    }
    

    public final List listPlaceVisible() throws BasicException {
        return new StaticSentence(s, ((TableDBSentenceBuilder)getTableBuilder()).getListSQL(getTable()), null, this).list();
    }

    public final Place findPlaceByName(String name) throws BasicException {
        return (Place)  new PreparedSentence(s, ((TableDBSentenceBuilder)getTableBuilder()).getListSQL(getTable())+" WHERE NAME = ?", SerializerWriteString.INSTANCE, this).find(name);
    }

    @Override
    public Class getSuportClass() {
        return Place.class;
    }

}
